﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 模块资源
    /// </summary>
    public class ModuleController : BaseAdminController
    {
        #region 构造函数注入
        private ModuleApp _module;
        public ModuleController(ModuleApp module)
        {
            this._module = module;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 分页数据查询响应参数
        /// </summary>
        public class GetPageRespone
        {
            /// <summary>
            /// 查询响应数据
            /// </summary>
            public IEnumerable<ModuleEntity> rows { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int records { get; set; }
        }
        /// <summary>
        /// 保存实体请求参数
        /// </summary>
        public class SaveFormRequest
        {
            /// <summary>
            /// 模块实体
            /// </summary>
            public ModuleEntity moduleEntity { get; set; }
            /// <summary>
            /// 按钮集
            /// </summary>
            public List<ModuleButtonEntity> moduleButtonList { get; set; }
        }
        #endregion

        #region 获取数据 - 模块功能
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetPageRespone> GetPageList(string queryjson, string pagination)
        {
            var paginationObj = pagination.ToObject<Pagination>();
            var data = _module.GetPageList(queryjson, paginationObj);
            return Success(new GetPageRespone()
            {
                rows = data,
                records = paginationObj.records
            });
        }
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<ModuleEntity>> GetTreeList(string queryjson)
        {
            var list = _module.GetTreeList(queryjson);
            return Success(list);
        }
        #endregion

        #region 获取数据 - 模块按钮

        /// <summary>
        /// 获取功能模块按钮数据列表
        /// </summary>
        /// <param name="id">模块实体主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<ModuleButtonEntity>> GetButtonList(string id)
        {
            var data = _module.GetButtonList(id);
            return Success(data);
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut, Logging("系统功能保存")]
        public ResParameter SaveForm(SaveFormRequest request)
        {
            var (isSuccess, msg) = _module.SaveForm(request.moduleEntity, request.moduleButtonList);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request">模块实体</param>
        /// <returns></returns>
        [HttpPut, Logging("系统功能删除")]
        public ResParameter DeleteForm(ModuleEntity request)
        {
            var (isSuccess, msg) = _module.DeleteForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 更改状态
        /// </summary>
        /// <param name="request">模块实体</param>
        /// <returns></returns>
        [HttpPut, Logging("系统功能状态更改")]
        public ResParameter ChangeState(ModuleEntity request)
        {
            var (isSuccess, msg) = _module.ChangeState(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }

        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateExist(string id, string field, string value)
        {
            var res = _module.ValidateExist(id, field, value);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        #endregion
    }
}
